
package com.trydofor.id.exec;

import org.apache.mina.filter.firewall.Subnet;

import com.trydofor.id.doer.shell.ArgHelp;
import com.trydofor.id.doer.shell.Command;
import com.trydofor.id.mina.WhitelistFilter;

/**
 * @author : Shi Rongjiu (www.trydofor.com)
 */
public final class WhitelistCmd {
    
    private WhitelistCmd() {
    }
    
    /**
     * show current white list
     * 
     * @return the command
     */
    public static Command listWhite() {
        return new Command("listWhite", "show the current white list");
    }
    
    /**
     * add a sub-net
     * 
     * @return the command
     */
    public static Command addWhite() {
        Command cmd = new Command("addWhite", "add a subnet to the white list");
        ArgHelp arg = new ArgHelp("subnet");
        arg.setRequired(true);
        arg.setPattern("^\\d{1,3}(\\.\\d{1,3}){3}(/\\d{1,2})?$");
        cmd.addArgument(arg);
        return cmd;
    }
    
    /**
     * delete a sub-net
     * 
     * @param whitelist the white list
     * @return the command
     */
    public static Command delWhite(final WhitelistFilter whitelist) {
        final Command cmd = new Command("delWhiteList", "delete a subnet to the white list") {
            
            @Override
            public void refreshArgs() {
                ArgHelp arg = new ArgHelp("subnet");
                arg.setRequired(true);
                arg.setEditable(false);
                for (Subnet net : whitelist.copyWhiteList()) {
                    arg.addOption(net.toString());
                }
                this.addArgument(arg);
            }
        };
        
        return cmd;
    }
}
